package com.atguigu.spring.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Aspect
@Component
public class AroundAspect {
    @Pointcut("execution(int com.atguigu.spring.aop.calculator.MathCalculator.*(..))")
    public void PiontCut() {
    }

    /*
    环绕通知的固定写法：public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{


    }

    */
    @Around("PiontCut()")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {

        //获取目标方法参数
        Object[] args = pjp.getArgs();
        System.out.println("环绕通知-----前置通知" + Arrays.toString(args));
        Object proceed = null;
        try {
            //接受传入参数的proceed，实现修改“目标方法执行时的参数”
            proceed = pjp.proceed(args);//继续执行目标方法；反射method.invoke()
            System.out.println("环绕通知-----返回通知   返回值 = " + proceed);
        } catch (Exception e) {
            System.out.println("环绕通知-----异常通知" + e.getMessage());
        } finally {
            System.out.println("环绕通知-----后置通知");
        }

        //获取被代理的方法
        String methodName = pjp.getSignature().getName();

        //获取被代理的方法返回值类型
        Class returnType = pjp.getSignature().getDeclaringType();

        //修改返回值
        return proceed;
    }
}


